package com.ks.core.framework.cpsupload.demos.pay;

import java.util.Map;

// 5. 微信支付策略实现
public class WechatPaymentStrategy extends PaymentStrategy {
    
    // 支付必需字段
    private static final String[] PAY_REQUIRED_FIELDS = {"appId", "mchId", "amount", "openId"};
    // 退款必需字段
    private static final String[] REFUND_REQUIRED_FIELDS = {"transactionId", "refundAmount"};
    
    @Override
    protected Map<String, Object> doOperation( PaymentOperation operation,  Map<String, Object> params ) throws Exception {
        switch (operation) {
            case PAY:
                return callWechatPayAPI(params);
            case REFUND:
                return callWechatRefundAPI(params);
            case QUERY:
                return callWechatQueryAPI(params);
            default:
                throw new UnsupportedOperationException("Unsupported operation");
        }
    }
    
    @Override
    protected void validateParams( PaymentOperation operation, Map<String, Object> params ) throws IllegalArgumentException {
        String[] requiredFields;
        switch (operation) {
            case PAY:
                requiredFields = PAY_REQUIRED_FIELDS;
                break;
            case REFUND:
                requiredFields = REFUND_REQUIRED_FIELDS;
                break;
            case QUERY:
                requiredFields = new String[]{"transactionId"};
                break;
            default:
                throw new IllegalArgumentException("无效操作类型");
        }
        
        for (String field : requiredFields) {
            if (!params.containsKey(field) || params.get(field) == null) {
                throw new IllegalArgumentException("缺少必需字段: " + field);
            }
        }
    }
    
    @Override
    protected UnifiedResponse buildUnifiedResponse( PaymentOperation operation,  Map<String, Object> rawResponse ) {
        switch (operation) {
            case PAY:
                return UnifiedResponse.success("微信支付成功", Map.of(
                    "transactionId", rawResponse.get("wx_transaction_id"),
                    "amount", rawResponse.get("total_fee")
                ));
            case REFUND:
                return UnifiedResponse.success("微信退款成功", Map.of(
                    "refundId", rawResponse.get("wx_refund_id"),
                    "refundAmount", rawResponse.get("refund_fee")
                ));
            case QUERY:
                return UnifiedResponse.success("微信查询成功", Map.of(
                    "status", rawResponse.get("trade_state"),
                    "amount", rawResponse.get("total_fee")
                ));
            default:
                return UnifiedResponse.fail("OPERATION_ERROR", "不支持的操作类型");
        }
    }
    
    @Override
    public UnifiedResponse handleCallback(Map<String, String> callbackParams) {
        // 验证微信回调签名
        if (!verifyWechatSignature(callbackParams)) {
            return UnifiedResponse.fail("SIGNATURE_FAIL", "签名验证失败");
        }
        
        // 处理业务逻辑
        String status = callbackParams.get("result_code");
        if ("SUCCESS".equals(status)) {
            return UnifiedResponse.success("回调处理成功", Map.of(
                "transactionId", callbackParams.get("transaction_id"),
                "paymentTime", callbackParams.get("time_end")
            ));
        } else {
            return UnifiedResponse.fail("CALLBACK_FAIL", callbackParams.get("err_code_des"));
        }
    }
    
    private boolean verifyWechatSignature(Map<String, String> params) {
        // 实现签名校验逻辑
        return true;
    }
    
    // 以下为模拟第三方API调用
    private Map<String, Object> callWechatPayAPI(Map<String, Object> params) {
        System.out.println("调用微信支付API，参数: " + params);
        return Map.of(
            "wx_transaction_id", "WX" + System.currentTimeMillis(),
            "total_fee", params.get("amount")
        );
    }
    
    private Map<String, Object> callWechatRefundAPI(Map<String, Object> params) {
        System.out.println("调用微信退款API，参数: " + params);
        return Map.of(
            "wx_refund_id", "RF" + System.currentTimeMillis(),
            "refund_fee", params.get("refundAmount")
        );
    }
    
    private Map<String, Object> callWechatQueryAPI(Map<String, Object> params) {
        System.out.println("调用微信查询API，参数: " + params);
        return Map.of(
            "trade_state", "SUCCESS",
            "total_fee", 100.00
        );
    }
}
